/*
 * @(#)AgentPropertiesDialog.java
 *
 *
 * ver 1.0 Oct 21, 2007 ggranum
 */
package org.testfire.agent.util;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import org.testfire.agent.AgentProperties;

public class AgentPropertiesDialog extends JDialog
{

    boolean userCanceled = false;

    private String title;

    private String message;

    public static void main( String[] args )
    {
        new AgentPropertiesDialog().setVisible( true );
    }

    public AgentPropertiesDialog()
    {
        this( null, "Please populate the application settings:", "Please populate the application settings:" );
    }

    public AgentPropertiesDialog( Frame frame, String title, String message )
    {
        super( frame, title, true );
        try
        {
            this.title = title;
            this.message = "<html><center>" + message.replaceAll( "\\n", "<br>" ) + "</center></html>";
            jbInit();
            CenterComponent( frame, this );
            pack();
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }

    public boolean getUserCanceled()
    {
        return userCanceled;
    }

    private void jbInit( ) throws Exception
    {
        // agentName
        // agentAddress
        // serverAddress
        // listenPort
        // localMode
        // testsDirectory
        // classesDirectory
        // saveDirectory
        // description
        final AgentProperties props = AgentProperties.getInstance();

        ArrayList<JComponent> components = new ArrayList<JComponent>();

        final JLabel jlMessage = new JLabel( this.message );
        this.setTitle( title );

        final JLabel jlAgentName = new JLabel( "Agent Name: " );
        final JTextField jtfAgentName = new JTextField(props.getAgentName());
        components.add( jlAgentName );
        components.add( jtfAgentName );

        final JLabel jlAgentAddress = new JLabel( "Agent Address:" );
        final JTextField jtfAgentAddress = new JTextField(props.getAgentAddress());
        components.add( jlAgentAddress );
        components.add( jtfAgentAddress );

        final JLabel jlAgentPort = new JLabel( "Agent Listen Port:" );
        final JTextField jtfAgentPort = new JTextField( String.valueOf(props.getListenPort()) );
        components.add( jlAgentPort );
        components.add( jtfAgentPort );

        final JCheckBox jckLocalMode = new JCheckBox( "Local-Only Mode?", props.getLocalMode() );
        components.add( null );
        components.add( jckLocalMode );

        final JLabel jlServerAddress = new JLabel( "Server Address:" );
        final JTextField jtfServerAddress = new JTextField( props.getServerAddress() );
        components.add( jlServerAddress );
        components.add( jtfServerAddress );

        final JLabel jlSaveDirectory = new JLabel( "Save Directory: " );
        final JTextField jtfSaveDirectory = new JTextField(props.getSaveDirectory().getCanonicalPath());
        components.add( jlSaveDirectory );
        components.add( jtfSaveDirectory );

        final JLabel jlWorkingDirectory = new JLabel( "Working Directory:" );
        final JTextField jtfWorkingDirectory = new JTextField(props.getWorkingDirectory().getCanonicalPath());
        components.add( jlWorkingDirectory );
        components.add( jtfWorkingDirectory );

        final JLabel jlDescription = new JLabel( "Description: " );
        final JTextArea jtfDescription = new JTextArea( props.getDescription() );
        components.add( jlDescription );
        components.add( jtfDescription );

        final JPanel panel1 = new JPanel();
        final BorderLayout borderLayout1 = new BorderLayout();
        final JPanel jpSouth = new JPanel();
        final JPanel jpCenter = new JPanel();
        final JButton jbOk = new JButton();
        final JButton jbCancel = new JButton();

        panel1.setLayout( borderLayout1 );

        this.setDefaultCloseOperation( javax.swing.WindowConstants.HIDE_ON_CLOSE );
        this.setFont( new java.awt.Font( "Dialog", 0, 12 ) );
        this.setModal( true );
        this.setResizable( true );

        jbOk.setText( "Okay" );
        jbOk.addActionListener( new java.awt.event.ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {

                try
                {
                    File f = new File(jtfSaveDirectory.getText());
                    props.setSaveDirectory(f);
                }
                catch ( Exception e1 )
                {
                    JOptionPane.showMessageDialog( AgentPropertiesDialog.this, "Invalid Save Directory", "Invalid Save Directory", JOptionPane.ERROR_MESSAGE, null);
                    jtfSaveDirectory.requestFocus(false);
                }
                try
                {
                    File f = new File(jtfWorkingDirectory.getText());
                    props.setWorkingDirectory(f);
                }
                catch ( Exception e1 )
                {
                    JOptionPane.showMessageDialog( AgentPropertiesDialog.this, "Invalid Working Directory", "Invalid Working Directory", JOptionPane.ERROR_MESSAGE, null);
                    jtfWorkingDirectory.requestFocus(false);
                }

                props.setLocalMode( jckLocalMode.isSelected());

                props.setAgentName(jtfAgentName.getText());
                props.setAgentAddress(jtfAgentAddress.getText());
                try
                {
                    int port = Integer.parseInt(jtfAgentPort.getText());
                    if(port < 0 || port > 65536){
                        throw new Exception("Invalid port.");
                    }
                    props.setListenPort( port );
                }
                catch ( Exception e1 )
                {
                    JOptionPane.showMessageDialog( AgentPropertiesDialog.this, "Invalid Port Number", "Invalid Port Number", JOptionPane.ERROR_MESSAGE, null);
                    jtfAgentPort.requestFocus(false);
                }
                props.setDescription( jtfDescription.getText( ));
                props.setServerAddress( jtfServerAddress.getText());
                try
                {
                    props.writeOut();
                }
                catch ( IOException e1 )
                {
                    e1.printStackTrace();
                }
                AgentPropertiesDialog.this.setVisible( false );
                AgentPropertiesDialog.this.dispose();



            }
        } );
        jbCancel.setText( "Cancel" );
        jbCancel.addActionListener( new java.awt.event.ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                AgentPropertiesDialog.this.userCanceled = true;
                AgentPropertiesDialog.this.setVisible( false );
                AgentPropertiesDialog.this.dispose();
            }
        } );
        jpCenter.setLayout( null );

        jlMessage.setFont( new java.awt.Font( "Dialog", 0, 11 ) );
        jlMessage.setHorizontalAlignment( SwingConstants.CENTER );
        jlMessage.setHorizontalTextPosition( SwingConstants.CENTER );
        jlMessage.setVerticalAlignment( javax.swing.SwingConstants.CENTER );
        jlMessage.setBounds( new Rectangle( 21, 5, 312, 61 ) );
        panel1.setPreferredSize( new Dimension( 600, 500 ) );

        int labelWidth = 200;
        int fieldWidth = 300;
        int fieldX = 20;
        int fieldY = 80;
        int yDelta = 25;
        for ( int i = 0, L = components.size(); i < L; i++ )
        {
            JComponent labelComp = components.get( i++ );
            JComponent fieldComp = components.get( i );
            if ( labelComp != null )
            {
                labelComp.setBounds( new Rectangle( fieldX, fieldY, labelWidth, 15 ) );
                jpCenter.add( labelComp, null );
            }
            if ( fieldComp != null )
            {
                if(fieldComp instanceof JTextArea)
                {
                    fieldComp.setBounds( new Rectangle( fieldX + labelWidth + 10, fieldY, fieldWidth, 5*yDelta ) );
                    fieldY += 4*yDelta;
                }
                else
                {
                    fieldComp.setBounds( new Rectangle( fieldX + labelWidth + 10, fieldY, fieldWidth, 20 ) );
                }
            }
            jpCenter.add( fieldComp, null );            
            fieldY += yDelta;
        }
        jpCenter.setPreferredSize( new Dimension( 600, 500 ) );
        getContentPane().add( panel1 );
        jpSouth.add( jbOk, null );
        jpSouth.add( jbCancel, null );
        panel1.add( jpCenter, BorderLayout.CENTER );
        panel1.add( jpSouth, BorderLayout.SOUTH );
    }

    public static void CenterComponent( Component comp )
    {
        CenterComponent( null, comp );
    }

    public static void CenterComponent( Component parent, Component comp )
    {
        Dimension dlgSize = comp.getPreferredSize();
        Dimension frmSize;
        Point loc;
        if ( parent != null )
        {
            frmSize = parent.getSize();
            loc = parent.getLocation();
        }
        else
        {
            frmSize = Toolkit.getDefaultToolkit().getScreenSize();
            loc = new Point( 0, 0 );
        }
        comp.setLocation( frmSize.width / 2 - dlgSize.width / 2 + loc.x,
            frmSize.height / 2 - dlgSize.height / 2 + loc.y );
        comp.paintAll( comp.getGraphics() );

    }
}

// eof: AgentPropertiesDialog.java